package com.yuanlrc.base.service.common;

import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.dao.common.GoodsDao;
import com.yuanlrc.base.entity.common.Goods;
import com.yuanlrc.base.entity.common.HomeUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物品管理service
 */
@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    /**
     * 物品添加或修改，当id不为空就修改
     * @param goods
     * @return
     */
    public Goods save(Goods goods){
        return goodsDao.save(goods);
    }


    /**
     * 搜索分类列表
     * @param pageBean
     * @param goods
     * @return
     */
    public PageBean<Goods> findlist(PageBean<Goods> pageBean,Goods goods){
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        exampleMatcher=exampleMatcher.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        exampleMatcher=exampleMatcher.withIgnorePaths("flag");
        Example<Goods> example=Example.of(goods,exampleMatcher);
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageable = PageRequest.of(pageBean.getCurrentPage() - 1, pageBean.getPageSize(), sort);
        Page<Goods> findAll = goodsDao.findAll(example, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Goods findById(Long id){
        return goodsDao.find(id);
    }

    /**
     * 物品分类删除
     * @param id
     */
    public void delete(Long id){
        goodsDao.deleteById(id);
    }

    public List<Goods> findAll(){
        return goodsDao.findAll();
    }

    /**
     * 根据用户查找物品
     * @param homeUser
     * @return
     */
    public List<Goods> findByHomeUser(HomeUser homeUser){
        return goodsDao.findByHomeUser(homeUser);
    }



    /**
     * 根据用户id和商品id查询
     * @param id
     * @param homeuseId
     * @return
     */
    public  Goods finds(Long id,Long homeuseId){
        return goodsDao.finds(id,homeuseId);
    }
}
